package com.woniuxy.project.campus.account.controller;

import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.project.campus.account.model.form.*;
import com.woniuxy.project.campus.account.model.param.SaddParam;
import com.woniuxy.project.campus.account.redisClint.RedisUtil;
import com.woniuxy.project.campus.util.Result;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("/redis")
public class RedisController {

    @Resource
    private RedisUtil redisUtil;

    @PostMapping("/getString")
    public Result getString(@RequestParam String key) throws Exception {
        String string = redisUtil.getString(key);
        return Result.success(string);
    }

    @PostMapping("/set")
    public Result set(@RequestBody StringKAndVForm form ) {
        com.woniuxy.project.campus.account.model.param.StringKAndVParam param = BeanUtil.toBean(form, com.woniuxy.project.campus.account.model.param.StringKAndVParam.class);
        redisUtil.set(param.getKey(), param.getValue());
        return Result.success("添加成功");
    }
    @PostMapping("/set1")
    public Result set1(@RequestBody Set1Foram form ) {
        com.woniuxy.project.campus.account.model.param.Set1Param param = BeanUtil.toBean(form, com.woniuxy.project.campus.account.model.param.Set1Param.class);
        redisUtil.set(param.getKey(), param.getValue(), param.getTimeout());
        return Result.success("添加成功");
    }

    @PostMapping("/delete")
    public Result delete(@RequestParam String key) {
        redisUtil.delete(key);
        return Result.success("删除成功");
    }
    @PostMapping("/hasKey")
    public Result hasKey(@RequestParam String key) {
        return Result.success(redisUtil.hasKey(key));
    }
    @PostMapping("/ttl")
    public Result ttl(@RequestParam String key) {
        return Result.success(redisUtil.ttl(key));
    }
    @PostMapping("/increment")
    public Result increment(@RequestParam String key) {
        Long increment = redisUtil.increment(key);
        return Result.success(increment);
    }

    @PostMapping("/sadd")
    public Result sadd(@RequestBody SaddForm form) {
        com.woniuxy.project.campus.account.model.param.SaddParam param = BeanUtil.toBean(form, SaddParam.class);
        redisUtil.sadd(param.getKey(), param.getSet());
        return Result.success("添加成功");
    }
    @PostMapping("/smembers")
    public Result smembers(@RequestParam String key) {
        Set<String> smembers = redisUtil.smembers(key);
        return Result.success(smembers);
    }
    @PostMapping("/srem")
    public Result srem(@RequestBody StringKAndVForm form) {
        com.woniuxy.project.campus.account.model.param.StringKAndVParam param = BeanUtil.toBean(form, com.woniuxy.project.campus.account.model.param.StringKAndVParam.class);
        redisUtil.srem(param.getKey(), param.getValue());
        return Result.success("删除成功");
    }
    @PostMapping("/score")
    public Result score(@RequestBody StringKAndVForm form) {
        com.woniuxy.project.campus.account.model.param.StringKAndVParam param = BeanUtil.toBean(form, com.woniuxy.project.campus.account.model.param.StringKAndVParam.class);
        Double score = redisUtil.score(param.getKey(), param.getValue());

        System.out.println("s___________________________________"+score);
        return Result.success(score);
    }
    @PostMapping("/zadd")
    public Result zadd(@RequestBody Set1Foram form) {
        com.woniuxy.project.campus.account.model.param.Set1Param param = BeanUtil.toBean(form, com.woniuxy.project.campus.account.model.param.Set1Param.class);
        redisUtil.zadd(param.getKey(), param.getValue(), param.getTimeout());
        return Result.success("添加成功");
    }
    @PostMapping("/zincrby")
    public Result zincrby(@RequestBody Set1Foram form) {
        com.woniuxy.project.campus.account.model.param.Set1Param param = BeanUtil.toBean(form, com.woniuxy.project.campus.account.model.param.Set1Param.class);
        redisUtil.zincrby(param.getKey(), param.getValue(), param.getTimeout());
        return Result.success("添加成功");
    }
    @PostMapping("/zrevrange")
    public Result zrevrange(@RequestBody ZrevrangeForm form) {
        com.woniuxy.project.campus.account.model.param.ZrevrangeParam param = BeanUtil.toBean(form, com.woniuxy.project.campus.account.model.param.ZrevrangeParam.class);
        Set<String> zrevrange = redisUtil.zrevrange(param.getKey(), param.getStart(), param.getEnd());
        return Result.success(zrevrange);
    }
    @PostMapping("/zcard")
    public Result zcard(@RequestParam String key) {
        Long zcard = redisUtil.zcard(key);
        return Result.success(zcard);
    }
    @PostMapping("/zrem")
    public Result zrem(@RequestBody StringKAndVForm form) {
        com.woniuxy.project.campus.account.model.param.StringKAndVParam param = BeanUtil.toBean(form, com.woniuxy.project.campus.account.model.param.StringKAndVParam.class);
        redisUtil.zrem(param.getKey(), param.getValue());
        return Result.success("删除成功");
    }
    @PostMapping("/getZSetMembers")
    public Result getZSetMembers(@RequestParam String key) {
        Set<String> zsetMembers = redisUtil.getZSetMembers(key);
        return Result.success(zsetMembers);
    }
    @PostMapping("/setObject")
    public Result setObject(@RequestBody SetObjectForm form) throws Exception {
        com.woniuxy.project.campus.account.model.param.SetObjectParam param = BeanUtil.toBean(form, com.woniuxy.project.campus.account.model.param.SetObjectParam.class);
        redisUtil.setObject(param.getKey(), param.getValue());
        return Result.success("添加成功");
    }
    @PostMapping("/setObject1")
    public Result setObject1(@RequestBody SetObject1Form form) throws Exception {
        com.woniuxy.project.campus.account.model.param.SetObject1Param param = BeanUtil.toBean(form, com.woniuxy.project.campus.account.model.param.SetObject1Param.class);
        redisUtil.setObject(param.getKey(), param.getValue(), param.getTime());
        return Result.success("添加成功");
    }
    @PostMapping("/getObject")
    public Result getObject(@RequestParam String key) throws Exception {
        Object object = redisUtil.getObject(key);
        return Result.success(object);
    }
    @PostMapping("/setHashMap")
    public Result setHashMap(@RequestBody SetHashMapForm form) throws Exception {
        com.woniuxy.project.campus.account.model.param.SetHashMapParam param = BeanUtil.toBean(form, com.woniuxy.project.campus.account.model.param.SetHashMapParam.class);
        redisUtil.setHashMap(param.getKey(), param.getMap());
        return Result.success("添加成功");
    }
    @PostMapping("/hashIncrement")
    public Result hashIncrement(@RequestBody com.woniuxy.project.campus.account.model.param.HashIncrementParam form) throws Exception {
        com.woniuxy.project.campus.account.model.param.HashIncrementParam param = BeanUtil.toBean(form, com.woniuxy.project.campus.account.model.param.HashIncrementParam.class);
        redisUtil.hashIncrement(param.getKey(), param.getHashKey(), param.getNumber());
        return Result.success("添加成功");
    }
    @PostMapping("/getHashMap")
    public Result getHashMap(@RequestBody StringKAndVForm form) throws Exception {
        com.woniuxy.project.campus.account.model.param.StringKAndVParam param = BeanUtil.toBean(form, com.woniuxy.project.campus.account.model.param.StringKAndVParam.class);
        Object object = redisUtil.getHashMap(param.getKey(), param.getValue());
        return Result.success(object);
    }
    @PostMapping("/getHashMapAll")
    public Result<HashMap<Object, Object>> getHashMapAll(@RequestParam String key) throws Exception {
        HashMap<Object, Object> hashMapAll = redisUtil.getHashMapAll(key);
        return Result.success(hashMapAll);
    }
    @PostMapping("/deleteHashMap")
    public Result deleteHashMap(@RequestParam String key) throws Exception {
        redisUtil.deleteHashMap(key);
        return Result.success("删除成功");
    }
    @PostMapping("/saveHashMap")
    public Result saveHashMap(@RequestBody SaveHashMapForm form ) throws Exception {
        com.woniuxy.project.campus.account.model.param.SaveHashMapParam param = BeanUtil.toBean(form, com.woniuxy.project.campus.account.model.param.SaveHashMapParam.class);
        redisUtil.saveHashMap(param.getKey(), param.getMap());
        return Result.success("添加成功");
    }

    @PostMapping("/incrementCdoe")
    public Result incrementCdoe(@RequestParam String key) {
        Long increment = redisUtil.incrementCdoe(key);
        return Result.success(increment);
    }

}
